﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //TODO : 修改服务器地址
    //新疆电信 ： 202.100.170.77
    //陕西电信 ： 117.34.65.39
    const string SERVER_ADDR = "202.100.170.77";

    const int SERVER_PORT = 8099;

    const string SMS_SALT = "x%i^n&l*i)yixunsms";

    const string CODE_PHONE = "%phone%",
        CODE_CHECK = "%check%",
        CODE_VALIDATE = "%validate%",
        CODE_ID = "%id%";

    const string REQUEST_CODE = "<op><accountInfo login='" + CODE_PHONE + "' checkCode='" + CODE_CHECK + "' id='" + CODE_ID + "' /></op>",
        RESPONSE_CODE = "<op><validateAccount login='" + CODE_PHONE + "' checkCode='" + CODE_CHECK + "' validateCode='" + CODE_VALIDATE + "' id='" + CODE_ID + "' /></op>";




    protected void Page_Load(object sender, EventArgs e)
    {
       
    }


    protected void getCode_Click(object sender, EventArgs e)
    {
        string request = getCheckCodeRequest(tbNum.Text);
        if (request != null)
        {
            string response = SocketHelper.sendRequest(request, SERVER_ADDR, SERVER_PORT);
            if (response != null)
            {
                Response.Write(response.IndexOf("sendSuccess='Y'") > 0 ? "<script>alert('短信验证码已经发送到你的手机，你需要在90秒内输入到验证码框然后发送')</script>" : "服务器未发送短信验证码，返回内容：" + response);
                //responseLabel.Text = response;
            }
            else
            {
                Response.Write("<script>alert('程序出现错误，或者服务器响应错误！')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('手机号码长度错误')</script>");
        }
        //requestLabel.Text = request;
        
    }

    protected void validCode_Click(object sender, EventArgs e)
    {
        string response = getCheckCodeResponse(tbNum.Text, tbCode.Text);
        if (response != null)
        {
            string serverResponse = SocketHelper.sendRequest(response, SERVER_ADDR, SERVER_PORT);
            if (serverResponse != null)
            {
                Response.Write("OK".Equals(getSubStr(serverResponse, "resCode='", "'")) ? "<script>alert('验证成功')</script>" : "验证错误！服务器返回数据：" + serverResponse);
                //responseLabel.Text = serverResponse;
            }
            else
            {
                Response.Write("<script>alert('程序出现错误，或者服务器响应错误！')</script>");
                //Response.Write("程序出现错误，或者服务器响应错误！");
            }
        }
        else
        {
            Response.Write("<script>alert('你的手机号码或者验证码非法！')</script>");
            //Response.Write("你的手机号码或者验证码非法！");
        }
    }

    public static string getSubStr(string origin, string findStart, string findEnd)
    {
        if (!string.IsNullOrEmpty(origin) && !string.IsNullOrEmpty(findStart) && !string.IsNullOrEmpty(findEnd))
        {
            int path = origin.IndexOf(findStart);
            if (path >= 0)
            {
                int pathE = origin.IndexOf(findEnd, path + findStart.Length);
                if (pathE > path)
                {
                    return origin.Substring(path + findStart.Length, pathE - path - findStart.Length);
                }
            }
        }
        return null;
    }

    public static string getCheckCodeResponse(string number, string code)
    {
        string checkCode = getCheckCode(number);
        if (checkCode != null && code.Length >= 6)
        {
            return RESPONSE_CODE.Replace(CODE_PHONE, number).Replace(CODE_CHECK, checkCode).Replace(CODE_ID, getTimeSpan() + "").Replace(CODE_VALIDATE, code);
        }
        return null;
    }


    public static string getCheckCodeRequest(string number)
    {
        string checkCode = getCheckCode(number);
        if (checkCode != null)
        {
            return REQUEST_CODE.Replace(CODE_PHONE, number).Replace(CODE_CHECK, checkCode).Replace(CODE_ID, getTimeSpan() + "");
        }
        return null;
    }

    public static string getCheckCode(string number)
    {
        if (number.Length == 11)
        {
            return GetMd5Hash(number + SMS_SALT);
        }
        return null;
    }

    public static int getTimeSpan()
    {
        TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
        return (int)Math.Floor(ts.TotalSeconds);

    }

    /// <summary>
    /// MD5
    /// </summary>
    /// <param name="md5Hash"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string GetMd5Hash(string input)
    {
        MD5 md5Hash = MD5.Create();
        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }
}